package util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {

  private static String FromEmail = "xxxxxxxxxxxxxxx";
  private static String EmailPassword = "xxxxxxxxxxxxxxx";
  private static String EmailSMTPhost = "smtp.qq.com";


  public static void sendMail(String toEmail) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String identifyCode =
        "<div style=\"width:660px;overflow:hidden;border-bottom:1px solid #bdbdbe;\">" +
            "<div style=\"height:52px;overflow:hidden;border:1px solid #464c51;background:#353b3f url(http://www.lofter.com/rsc/img/email/hdbg.png);\">"
            +
            "<a href=\"http://www.lofter.com?mail=qbclickbynoticemail_20120626_01\" target=\"_blank\" "
            +
            "style=\"display:block;width:144px;height:34px;margin:10px 0 0 20px;overflow:hidden;text-indent:-2000px;background:url(http://www.lofter.com/rsc/img/email/logo.png) no-repeat;\">UF邮箱验证</a>"
            +
//                "</div><div style=\"padding:24px 20px;\">您好，"+toEmail+"<br/><br/>LOFTER是一款\"专注兴趣、分享创作\"的轻博客产品，旨在为\"热爱记录生活、追求时尚品质、崇尚自由空间\"的你，打造一个全新而定展示平台！<br/><br/>" +
            "请点击下面链接激活账号，24小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>" +
            "<a href=\"http://localhost:8080\">请点击此链接验证</a>" +
            "<br/>如果以上链接无法点击，请把上面网页地址复制到浏览器地址栏中打开<br/><br/><br/>LOFTER，专注兴趣，分享创作<br/>" +
            sdf.format(new Date()) + "</div></div>";

    // 1. 创建参数配置, 用于连接邮件服务器的参数配置
    Properties props = new Properties();

//        设置邮件Debug功能
    props.setProperty("smtp.debug", "true");
    //设置是否开启邮件认证功能
    props.setProperty("mail.smtp.auth", "true");
    //设置SMTP端口
    props.put("mail.smtp.port", 25);
    //设置邮件传输的协议
    props.setProperty("mail.transport.protocol", "smtp");
    //设置发送验证邮件的邮箱
    props.setProperty("mail.smtp.host", EmailSMTPhost);

    // 2. 根据配置创建会话对象, 用于和邮件服务器交互
    Session session = Session.getInstance(props);
    session.setDebug(true);

    // 3. 创建一封邮件
    Message msg = new MimeMessage(session);
    try {
      msg.setSubject("欢迎注册，还需激活验证");
      msg.setContent(identifyCode, "text/html;charset=utf-8");
      msg.setSentDate(new Date());
      msg.setFrom(new InternetAddress(FromEmail, "霸道总裁", "UTF-8"));

      Transport transport = session.getTransport();
      transport.connect(EmailSMTPhost, FromEmail, EmailPassword);

      transport.sendMessage(msg, new Address[]{new InternetAddress(toEmail)});

      transport.close();
    } catch (UnsupportedEncodingException | MessagingException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {

    String toEmail = "xxxxxxxxxxxxxx";

    //调用静态方法
    SendEmail.sendMail(toEmail);
  }
}
